package com.sc.outpatientsys.controller;

import com.sc.outpatientsys.entity.Admin;
import com.sc.outpatientsys.service.IAdminService;
import com.sc.outpatientsys.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController{

    @Autowired
    private IAdminService adminService;
    @RequestMapping("/reg")
    public JsonResult<Void> reg(Admin admin){
        adminService.reg(admin);
        log.info(admin.toString());
        return new JsonResult<>(OK);
    }

    @RequestMapping("/login")
    public JsonResult<Admin> login(String username, String password, HttpSession session){
        log.info(username+" :: "+password);
        Admin data = adminService.login(username, password);
        session.setAttribute("id",data.getAd_ID());
        session.setAttribute("username",data.getUserName());
        log.info(getIdFromSession(session).toString());
        log.info(getUserNameFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/findById")
    public JsonResult<Admin> findByID(int ad_ID){
        Admin data = adminService.findById(ad_ID);
        return new JsonResult<>(OK, data);
    }

    @RequestMapping("/findByName")
    public JsonResult<Admin> findByName(String username){
        Admin data = adminService.findByName(username);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/findAll")
    public JsonResult<List<Admin>> findAll(){
        List<Admin> data = adminService.findAll();
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/update")
    public JsonResult<Void> update(Admin admin){
        adminService.update(admin);
        return new JsonResult<>(OK);
    }
    @RequestMapping("/delete")
    public JsonResult<Void> delete(int ad_ID){
        adminService.delete(ad_ID);
        return new JsonResult<>(OK);
    }
}
